<?php
namespace app\api\controller\system;

use app\api\controller\Api;
use think\exception\ValidateException;

class File extends Api
{
    public function upload()
    {
        $post = $this->request->post();
        $dir = !empty($post['field']) ? $post['field'] : 'file';
        // 获取上传配置
        $systemConfigLogic = new \app\common\config\logic\ConfigSystem();
        $uploadConfig = $systemConfigLogic->getConfigItem("upload");

        $file = $this->request->file('file');
        if (empty($file)) return error([], '请选择要上传的文件');

        try {
            $rules = [
                "file" => ["fileSize:{$uploadConfig['size']}", "fileExt:{$uploadConfig['ext']}"],
            ];
            $messages = [
                "file.fileSize" => '文件不超过',
                "file.fileExt"  => '不支持的文件格式'
            ];
            $validateLogic = validate($rules, $messages);
            $checkRs = $validateLogic->check(['file' => $file]);
            if (!$checkRs) {
                return error([], $validateLogic->getError());
            }

            // 上传到本地服务器
            // $saveName = \think\facade\Filesystem::disk("image")->putFile($dir, $file);
            $saveName = \think\facade\Filesystem::disk("public")->putFile($dir, $file);

            if ($saveName) {
                $config = config('filesystem.disks');
                $saveName = $config['public']['url'] . str_ireplace('\\', '/', $saveName);
                $result['path'] = $this->request->domain() . $saveName;
                // 文件格式
                $result['extname'] = $file->extension();
                $result['name'] = $file->getOriginalName();
                $result['size'] = $file->getSize();
                $result['hash'] = $file->hash();
                $result['type'] = $file->getOriginalMime();
                // path
                $result['dir_path'] = $saveName;

                return success($result, '上传成功');
            } else {
                return error([], '上传失败');
            }
        } catch (ValidateException $e) {
            return error([], $e->getMessage());
        }
    }

    // 批量上传
    public function batch()
    {
        $post = $this->request->post();
        $dir = !empty($post['field']) ? $post['field'] : 'file';
        // 获取上传配置
        $systemConfigLogic = new \app\common\config\logic\ConfigSystem();
        $uploadConfig = $systemConfigLogic->getConfigItem("upload");

        $files = $this->request->file();
        $result = [];
        foreach($files as $file){
            $item = [
                'name' => $file->getOriginalName(),
                'extname' => $file->extension(),
                'url' => ''
            ];

            $saveName = \think\facade\Filesystem::disk("image")->putFile($dir, $file);
            $config = config('filesystem.disks');
            $saveName = $config['image']['url'] . '/' . str_ireplace('\\', '/', $saveName);
            $item['url'] = $this->request->domain() . $saveName;

            $result['files'][] = $item;
        }

        return success($result);

    }
    
    public function remove()
    {
        $post = $this->request->post();
        $url = $post['file'];
        $data = parse_url($url);

        $config = config('filesystem.disks');
        $filePath = $config['image']['root'] .$data['path'];

        if(unlink($filePath)) {
            return success([], "删除成功");
        } else {
            return error([], "删除失败");
        }
    }
}
